#Project: Quality of District Representation in US House Committees
#Author: Josh Ryan
#Working Date: Winter 2021
#Task: Create some graphs and other descriptive statistics
#Related files: predictpartyunitywnom_stata_v3_021121.dta

library(foreign)
library(effects)
library(sciplot)
library(sandwich)
library(lmtest)   
library(BMS)
library(rJava)
library(lattice)
library(plyr)
library(lme4)
library(gmodels)
library(MASS)
library(stringr)
library("XLConnect")
library(reshape2)
library(xlsx)
library(dplyr)
library(doBy)
library(Hmisc)
library(stargazer)
library(xtable)
library(dplyr)
library(xtable)
library(foreign)
library(dotwhisker)#https://cran.r-project.org/web/packages/dotwhisker/vignettes/dotwhisker-vignette.html
library(berryFunctions) #for the insert row function
library(ggplot2)
library(gridExtra)
library(haven)
library(grid)
library(cowplot)
library(tigris)
library(tidycensus)
library(cdlTools)

#load working stata data
dat<-read_dta("predictpartyunitywnom_stata_v4_021721.dta")
ls(dat)

#Figure: Marginal Effect of District Partisanship Conditional on Percentage of Committee Seats Controlled by Majority Party and Committee Staff
#read in marginal effects
margins.panel.1<-read_dta("dispart_stafflogpermaj_margins_v2_040821.dta")
margins.panel.1.ind.1<-subset(margins.panel.1, permaj_ind==1)
margins.panel.1.ind.0<-subset(margins.panel.1, permaj_ind==0)

maj.party.comm.margins<-ggplot(margins.panel.1.ind.1, aes(value),alpha=1)+
  geom_line(aes(y=margin))+
  geom_line(linetype=2,aes(y= upperci,linetype="SDF"))+
  geom_line(linetype=2,aes(y= lowerci))+
  geom_hline(yintercept=0)+
  labs(color = NULL)+ labs(shape = NULL)+
  scale_x_continuous(breaks=seq(.52, .74, .02))+ coord_cartesian(xlim=c(.52, .8), clip = "off") +
  scale_y_continuous(breaks=seq(-10, 4, 2)) +
  coord_cartesian(ylim=c(-10, 4)) + theme_bw() +
    theme(panel.border = element_blank(), panel.grid.major = element_blank(), plot.margin = unit(c(5.5,12,5.5,5.5), "pt"),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))+
  theme(axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5))+
  xlab("Proportion of Committee Controlled by Maj. Party") + ylab("Marginal Effect of District Dem. Support")+
  ggtitle("Majority Party Committee Control")+
  theme(plot.title = element_text(hjust = 0.5))
maj.party.comm.margins

maj.party.comm.margins<-maj.party.comm.margins+
  geom_rug(data=dat, mapping=aes(x=permaj), color="black")+
  geom_rug(alpha=0.4) +
  geom_hline(yintercept=-10.22)
maj.party.comm.margins


maj.party.comm.margins.staff<-ggplot(margins.panel.1.ind.0, aes(value),alpha=1)+
  geom_line(aes(y=margin))+
  geom_line(linetype=2,aes(y= upperci,linetype="SDF"))+
  geom_line(linetype=2,aes(y= lowerci))+
  geom_hline(yintercept=0)+
  labs(color = NULL)+ labs(shape = NULL)+
  scale_x_continuous(breaks=seq(3.25, 5, .25))+ coord_cartesian(xlim=c(3.25, 5)) +
  scale_y_continuous(breaks=seq(-10, 4, 2)) +
  coord_cartesian(ylim=c(-10, 4)) + theme_bw() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))+
  theme(axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5))+
  xlab("Number of Committee Staff (Logged)") + ylab("Marginal Effect of District Dem. Support")+
  ggtitle("Committee Staff")+
  theme(plot.title = element_text(hjust = 0.5))
maj.party.comm.margins.staff

maj.party.comm.margins.staff<-maj.party.comm.margins.staff+
  geom_rug(data=dat, mapping=aes(x=stafflog), color="black")+
  geom_rug(alpha=0.4)+
  geom_hline(yintercept=-10.22)
maj.party.comm.margins.staff

library(gridExtra)
grid.arrange(maj.party.comm.margins, maj.party.comm.margins.staff, ncol=2)


#Figure: Marginal Effect of Absolute District Partisanship Conditional on Percentage of Committee Seats Controlled by Majority Party and Committee Staff

abs.dist.part<-read_dta("absdispart_permaj_margins_v1_040821.dta")

abs.dist.part.margins<-ggplot(abs.dist.part, aes(value),alpha=1)+
  geom_line(aes(y=margin))+
  geom_line(linetype=2,aes(y= upperci,linetype="SDF"))+
  geom_line(linetype=2,aes(y= lowerci))+
  geom_hline(yintercept=0)+
  labs(color = NULL)+ labs(shape = NULL)+
  scale_x_continuous(breaks=seq(.52, .74, .02))+ coord_cartesian(xlim=c(.52, .8), clip = "off") +
  scale_y_continuous(breaks=seq(-3, 2, 1)) +
  coord_cartesian(ylim=c(-3, 2)) + theme_bw() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), plot.margin = unit(c(5.5,12,5.5,5.5), "pt"),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))+
  theme(axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5))+
  xlab("Proportion of Committee Controlled by Maj. Party") + ylab("Marginal Effect of District Dem. Support")+
  ggtitle("")+
  theme(plot.title = element_text(hjust = 0.5))
abs.dist.part.margins

abs.dist.part.margins<-abs.dist.part.margins+
  geom_rug(data=dat, mapping=aes(x=permaj), color="black")+
  geom_rug(alpha=0.4) +
  geom_hline(yintercept=-3.08)
abs.dist.part.margins



#Figure: Marginal Effect of District Partisanship Conditional on Incumbent Vote Share

#read in marginal effects
voteshare.margins<-read_dta("voteshare_margins_v1_040821.dta")
voteshare.margins.ind.1<-subset(voteshare.margins, absoluteind==1)
voteshare.margins.ind.0<-subset(voteshare.margins, absoluteind==0)

voteshare.margins.0<-ggplot(voteshare.margins.ind.0, aes(value),alpha=1)+
  geom_line(aes(y=margin))+
  geom_line(linetype=2,aes(y= upperci,linetype="SDF"))+
  geom_line(linetype=2,aes(y= lowerci))+
  geom_hline(yintercept=0)+
  labs(color = NULL)+ labs(shape = NULL)+
  scale_x_continuous(breaks=seq(50, 100, 5))+ coord_cartesian(xlim=c(50, 100), clip = "off") +
  scale_y_continuous(breaks=seq(-5, 2, 1)) +
  coord_cartesian(ylim=c(-5, 2)) + theme_bw() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), plot.margin = unit(c(5.5,12,5.5,5.5), "pt"),
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))+
  theme(axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5))+
  xlab("Incumbent Vote Share in Previous Election") + ylab("Marginal Effect of District Dem. Support")+
  ggtitle("District Dem. Support")+
  theme(plot.title = element_text(hjust = 0.5))
voteshare.margins.0

voteshare.margins.0<-voteshare.margins.0+
  geom_rug(data=dat, mapping=aes(x=voteshare), color="black")+
  geom_rug(alpha=0.4) +
  geom_hline(yintercept=-5.11)
voteshare.margins.0


voteshare.margins.1<-ggplot(voteshare.margins.ind.1, aes(value),alpha=1)+
  geom_line(aes(y=margin))+
  geom_line(linetype=2,aes(y= upperci,linetype="SDF"))+
  geom_line(linetype=2,aes(y= lowerci))+
  geom_hline(yintercept=0)+
  labs(color = NULL)+ labs(shape = NULL)+
  scale_x_continuous(breaks=seq(50, 100, 5))+ coord_cartesian(xlim=c(50, 100)) +
  scale_y_continuous(breaks=seq(-5, 2, 1)) +
  coord_cartesian(ylim=c(-5, 2)) + theme_bw() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))+
  theme(axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5))+
  xlab("Incumbent Vote Share in Previous Election") + ylab("Marginal Effect of District Dem. Support")+
  ggtitle("Absolute District Dem. Support")+
  theme(plot.title = element_text(hjust = 0.5))
voteshare.margins.1

voteshare.margins.1<-voteshare.margins.1+
  geom_rug(data=dat, mapping=aes(x=voteshare), color="black")+
  geom_rug(alpha=0.4) +
  geom_hline(yintercept=-5.11)
voteshare.margins.1

library(gridExtra)
grid.arrange(voteshare.margins.0, voteshare.margins.1, ncol=2)


#Figure: Marginal Effect of District Partisanship Conditional on Legislator Terms Served--not shown in paper but mentioned in text
#read in marginal effects
numterms.margins<-read_dta("dispart_numterms_margins_v1_051722.dta")

numterms.margins.gph<-ggplot(numterms.margins, aes(value),alpha=1)+
  geom_line(aes(y=margin))+
  geom_line(linetype=2,aes(y= upperci,linetype="SDF"))+
  geom_line(linetype=2,aes(y= lowerci))+
  geom_hline(yintercept=0)+
  labs(color = NULL)+ labs(shape = NULL)+
  scale_x_continuous(breaks=seq(1, 31, 2))+ coord_cartesian(xlim=c(1, 31)) +
  scale_y_continuous(breaks=seq(-5, 6, 1)) +
  coord_cartesian(ylim=c(-5, 6)) + theme_bw() +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(colour = "black"))+
  theme(axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5))+
  xlab("Legislator Terms Served") + ylab("Marginal Effect of District Dem. Support")+
  ggtitle("")+
  theme(plot.title = element_text(hjust = 0.5))
numterms.margins.gph

#jitter num terms so it shows up better in rug plot
dat$numterms.jitter<-jitter(dat$numterms, factor = 5)

numterms.margins.gph<-numterms.margins.gph+
  geom_rug(data=dat, mapping=aes(x=numterms.jitter), color="black")+
  geom_rug(alpha=0.4) +
  geom_hline(yintercept=-5.11)+
  geom_rug(alpha = .2, sides="b", position = "jitter", aes(y = 0))

numterms.margins.gph
